﻿<%@ WebHandler Language="C#" Class="PreliminaryApply" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

//****************************************************************
//   臣信二手房
//   Copyright (c) 2010 Chenxin.
//****************************************************************
//****************************************************************
//作成者       :  zou.xiaochun
//创建日期     :  2010-07-8
//確認者       :  確認者
//功能概要　   :  预审贷款申请
//****************************************************************
public class PreliminaryApply : AjaxHandlerBase
{
    //构造函数
    public PreliminaryApply()
    {
        //添加预审贷款权限
        htTypePermit.Add("add", Constants.Permit_AddPreliminary);
        //更新预审贷款权限
        htTypePermit.Add("upd", Constants.Permit_UpdPreliminary);
    }

    override public void RequestProcess(HttpContext context)
    {
        try
        {
            //处理类型
            string strOp = context.Request["op"];
            string strRet = string.Empty;
            if (strOp != null)
            {
                switch (strOp.ToLower())
                {
                    //获取实体
                    //case "getmodel":
                    //    strRet = GetModel(context);
                    //    break;
                    //添加预审相关信息
                    case "add":
                        strRet = addPreliminary(context);
                        break;
                    //编辑预审相关信息
                    case "upd":
                        strRet = updPreliminary(context);
                        break;
                    //添加外接预审相关信息
                    case "qz":
                        strRet = qzPreliminary(context);
                        break;
                    default:
                        break;
                }
                context.Response.Write(strRet);
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    /// <summary>
    /// 获取实体
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    //private string GetModel(HttpContext context)
    //{
    //    try
    //    {
    //        string strId = context.Request["modelid"];
    //        string strType = context.Request["type"];
    //        Chenxin.Lib.House.Entity.House_BaseInfo housemodel = null;
    //        Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo Premodel = null;
    //        switch(strType.ToLower())
    //        {
    //            case "init":
    //                housemodel = Chenxin.Lib.House.DAL.House_BaseInfo.GetModel(new Guid(strId));
    //                break;
    //            case "editpre":
    //                Premodel = Chenxin.Lib.Business.DAL.t_Preliminary_BaseInfo.GetModel(new Guid(strId));
    //                break;
    //        }
    //        if (housemodel != null)
    //        {
    //            return retExecResult(Constants.ExecResult.Succ, "数据不存在，获取信息成功！", housemodel);
    //        }
    //        else if (Premodel != null)
    //        {
    //            return retExecResult(Constants.ExecResult.Succ, "数据不存在，获取信息成功！", Premodel);
    //        }
    //        else
    //        {
    //            return retExecResult(Constants.ExecResult.Error, "数据不存在，获取信息失败！", "");
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        return retExecResult(Constants.ExecResult.Error, ex.Message, "");
    //    }
    //}
    
    /// <summary>
    /// 申请预审
    /// </summary>
    /// <param name="context"></param>
    private string addPreliminary(HttpContext context)
    {
        //预审申请信息
        string preliminary = context.Request.Params["model"];
        //出售方信息
        string lsPreSaler = context.Request.Params["lsPreSaler"];
        //买方信息
        string lsPreBuyer = context.Request.Params["lsPreBuyer"];

        //预审信息业务类
        Chenxin.Lib.Business.BLL.PreliminaryBaseInfo bllPreliminary = new Chenxin.Lib.Business.BLL.PreliminaryBaseInfo();

        //出售方信息业务类
        PreSalerInfo bllPreSalerInfo = new PreSalerInfo();

        //买方操作业务类
        PreBuyerInfo bllPreBuyerInfo = new PreBuyerInfo();

        //出售方信息
        List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2> lsModelOfSaler = new List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2>();

        //买方信息
        List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1> lsModelOfBuyer = new List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>();

        //出售方信息列表
        if (!string.IsNullOrEmpty(lsPreSaler))
        {
            lsModelOfSaler = (List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2>)JsonHelp.Convert2Entity(lsPreSaler,
                                typeof(List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2>));
        }

        //买方信息列表
        if (!string.IsNullOrEmpty(lsPreBuyer))
        {
            lsModelOfBuyer = (List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>)JsonHelp.Convert2Entity(lsPreBuyer,
                                typeof(List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>));
        }

        //预审信息
        Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entPreliminary =
            (Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo)JsonHelp.Convert2Entity(preliminary,
                    typeof(Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo));

        Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entOldPreliminary = bllPreliminary.GetOldModel(entPreliminary.HouseId);
        //if (entOldPreliminary != null && (entOldPreliminary.PreliminaryStatus != null || entOldPreliminary.PreliminaryStatus != 0))
        //{
        //    return retExecResult(Constants.ExecResult.Error, "流程中已经申请过此预审信息！", "");
        //}

        //③买方信息判断身份证唯一
        if (lsModelOfBuyer.Count > 1)
        {
            for (int idx = 0; idx < lsModelOfBuyer.Count; idx++)
            {
                for (int index = 1; index < lsModelOfBuyer.Count; index++)
                {
                    if (idx + index >= lsModelOfBuyer.Count)
                    {
                        continue;
                    }
                    if (lsModelOfBuyer[idx].IDNum == lsModelOfBuyer[idx + index].IDNum)
                    {
                        return retExecResult(Constants.ExecResult.Error, "请不要填相同的贷款人信息！", "");
                    }
                }
            }
        }
        
        entPreliminary = CompletePreliminary(entPreliminary,"添加");

        //①预审信息录入
        bllPreliminary.Add(entPreliminary);
        string text = CurrentUserInfo.UserName + "：预审申请。房源：[" + entPreliminary.HouseCode + "]" + entPreliminary.Address;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_ApplyPreliminary"].ToString());

        //②出售方信息录入
        for (int idx = 0; idx < lsModelOfSaler.Count; idx++)
        {
            //预审ID
            (lsModelOfSaler[idx]).MainId = entPreliminary.RecordId;

            //标识ID
            (lsModelOfSaler[idx]).RecordId = Guid.NewGuid();
            bllPreSalerInfo.Add(lsModelOfSaler[idx]);
        }
        
        //③买方信息录入
        for (int idx = 0; idx < lsModelOfBuyer.Count; idx++)
        {
            //预审ID
            (lsModelOfBuyer[idx]).MainId = entPreliminary.RecordId;
            //标识ID
            (lsModelOfBuyer[idx]).RecordId = Guid.NewGuid();
            if (lsModelOfBuyer.Count == 1)
            {
                (lsModelOfBuyer[idx]).BOwner = true;
            }
            bllPreBuyerInfo.Add(lsModelOfBuyer[idx]);
        }
         return retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_add_PreliminarySucc, "");
    }

    //*******************************************
    //功能概要       ：更新预审相关信息
    //参数説明       ：context 请求上下文
    //返回值説明     ：程序处理信息提示
    //功能詳細       ：更新预审相关信息
    //作成者         ：zou.xiaochun
    //作成日         ：2010-07-19
    //*******************************************
    private string updPreliminary(HttpContext context)
    {
        //预审申请信息
        string preliminary = context.Request.Params["model"];
        //出售方信息
        string lsPreSaler = context.Request.Params["lsPreSaler"];
        //买方信息
        string lsPreBuyer = context.Request.Params["lsPreBuyer"];

        //预审信息业务类
        Chenxin.Lib.Business.BLL.PreliminaryBaseInfo bllPreliminary = new Chenxin.Lib.Business.BLL.PreliminaryBaseInfo();

        //出售方信息业务类
        PreSalerInfo bllPreSalerInfo = new PreSalerInfo();

        //买方操作业务类
        PreBuyerInfo bllPreBuyerInfo = new PreBuyerInfo();

        //出售方信息
        List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2> lsModelOfSaler = new List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2>();

        //买方信息
        List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1> lsModelOfBuyer = new List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>();

        //出售方信息列表
        if (!string.IsNullOrEmpty(lsPreSaler))
        {
            lsModelOfSaler = (List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2>)JsonHelp.Convert2Entity(lsPreSaler,
                                typeof(List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2>));
        }

        //买方信息列表
        if (!string.IsNullOrEmpty(lsPreBuyer))
        {
            lsModelOfBuyer = (List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>)JsonHelp.Convert2Entity(lsPreBuyer,
                                typeof(List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>));
        }

        //预审信息
        Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entPreliminary =
            (Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo)JsonHelp.Convert2Entity(preliminary,
                    typeof(Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo));

        //Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entOldPreliminary = bllPreliminary.GetModel(entPreliminary.RecordId);
        //if (entOldPreliminary.PreliminaryStatus < 10 || entOldPreliminary.PreliminaryStatus > 30)
        //{
        //    return retExecResult(Constants.ExecResult.Error, "流程中未到或者已过预审修改阶段！", "");
        //}

        //③买方信息判断身份证唯一
        if (lsModelOfBuyer.Count > 1)
        {
            for (int idx = 0; idx < lsModelOfBuyer.Count; idx++)
            {
                for (int index = 1; index < lsModelOfBuyer.Count; index++)
                {
                    if (idx + index >= lsModelOfBuyer.Count)
                    {
                        continue;
                    }
                    if (lsModelOfBuyer[idx].IDNum == lsModelOfBuyer[idx + index].IDNum)
                    {
                        return retExecResult(Constants.ExecResult.Error, "请不要填相同的贷款人信息！", "");
                    }
                }
            }
        }
        
        entPreliminary = CompletePreliminary(entPreliminary,"更新");

        //①签约报备信息更新
        bllPreliminary.Update(entPreliminary);
        string text = CurrentUserInfo.UserName + "：预审修改。房源：[" + entPreliminary.HouseCode + "]" + entPreliminary.Address;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_UpdatePreliminary"].ToString());

        //②删除出售方信息
        bllPreSalerInfo.DeleteByMainId(entPreliminary.RecordId);

        //③出售方信息录入
        for (int idx = 0; idx < lsModelOfSaler.Count; idx++)
        {
            //签约报备ID
            (lsModelOfSaler[idx]).MainId = entPreliminary.RecordId;
            //标识ID
            (lsModelOfSaler[idx]).RecordId = Guid.NewGuid();
            bllPreSalerInfo.Add(lsModelOfSaler[idx]);
        }

        //④删除买方信息
        bllPreBuyerInfo.DeleteByMainId(entPreliminary.RecordId);

        //⑤买方信息录入
        for (int idx = 0; idx < lsModelOfBuyer.Count; idx++)
        {
            //签约报备ID
            (lsModelOfBuyer[idx]).MainId = entPreliminary.RecordId;
            //标识ID
            (lsModelOfBuyer[idx]).RecordId = Guid.NewGuid();
            if (lsModelOfBuyer.Count == 1)
            {
                (lsModelOfBuyer[idx]).BOwner = true;
            }
            bllPreBuyerInfo.Add(lsModelOfBuyer[idx]);
        }

        return retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_Update_PreliminarySucc, "");
    }

    /// <summary>
    /// 申请外接预审
    /// </summary>
    /// <param name="context"></param>
    private string qzPreliminary(HttpContext context)
    {
        //预审申请信息
        string preliminary = context.Request.Params["model"];
        //出售方信息
        string lsPreSaler = context.Request.Params["lsPreSaler"];
        //买方信息
        string lsPreBuyer = context.Request.Params["lsPreBuyer"];

        //预审信息业务类
        Chenxin.Lib.Business.BLL.PreliminaryBaseInfo bllPreliminary = new Chenxin.Lib.Business.BLL.PreliminaryBaseInfo();

        //出售方信息业务类
        PreSalerInfo bllPreSalerInfo = new PreSalerInfo();

        //买方操作业务类
        PreBuyerInfo bllPreBuyerInfo = new PreBuyerInfo();

        //出售方信息
        List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2> lsModelOfSaler = new List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2>();

        //买方信息
        List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1> lsModelOfBuyer = new List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>();

        //出售方信息列表
        if (!string.IsNullOrEmpty(lsPreSaler))
        {
            lsModelOfSaler = (List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2>)JsonHelp.Convert2Entity(lsPreSaler,
                                typeof(List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo2>));
        }

        //买方信息列表
        if (!string.IsNullOrEmpty(lsPreBuyer))
        {
            lsModelOfBuyer = (List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>)JsonHelp.Convert2Entity(lsPreBuyer,
                                typeof(List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>));
        }

        //预审信息
        Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entPreliminary =
            (Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo)JsonHelp.Convert2Entity(preliminary,
                    typeof(Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo));

        Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entOldPreliminary = bllPreliminary.GetOldModelByAddress(entPreliminary.Address);
        if (entOldPreliminary != null && (entOldPreliminary.PreliminaryStatus != null || entOldPreliminary.PreliminaryStatus != 0))
        {
            return retExecResult(Constants.ExecResult.Error, "流程中已经申请过此预审信息！", "");
        }

        entPreliminary = CompletePreliminary(entPreliminary,"外接");

        //①预审信息录入
        bllPreliminary.Add(entPreliminary);

        //②出售方信息录入
        for (int idx = 0; idx < lsModelOfSaler.Count; idx++)
        {
            //预审ID
            (lsModelOfSaler[idx]).MainId = entPreliminary.RecordId;

            //标识ID
            (lsModelOfSaler[idx]).RecordId = Guid.NewGuid();
            bllPreSalerInfo.Add(lsModelOfSaler[idx]);
        }

        //③买方信息录入
        for (int idx = 0; idx < lsModelOfBuyer.Count; idx++)
        {
            //预审ID
            (lsModelOfBuyer[idx]).MainId = entPreliminary.RecordId;
            //标识ID
            (lsModelOfBuyer[idx]).RecordId = Guid.NewGuid();
            bllPreBuyerInfo.Add(lsModelOfBuyer[idx]);
        }

        return retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_add_PreliminarySucc, "");
    }
    
    //*******************************************
    //功能概要       ：填充预审信息字段
    //参数説明       ：context 请求上下文
    //返回值説明     ：预审信息实体
    //功能詳細       ：填充预审信息字段
    //作成者         ：zou.xiaochun
    //作成日         ：2010-07-09
    //*******************************************
    private Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo CompletePreliminary(Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entPreliminary,string status)
    {
        //添加预审信息
        if (entPreliminary.RecordId.Equals(Guid.Empty) && status == "添加")
        {
            entPreliminary.RecordId = Guid.NewGuid();
            entPreliminary.PreliminaryStatus = 10;
            entPreliminary.OperateQuanZ = false;
            entPreliminary.EmpCityId = CurrentUserInfo.CityId;
            entPreliminary.EmpCityName = CurrentUserInfo.CityName;
            entPreliminary.EmpCompanyId = CurrentUserInfo.CompanyId;
            entPreliminary.EmpCompanyName = CurrentUserInfo.CompanyName;
            entPreliminary.AppCityId = CurrentUserInfo.CityId;
            entPreliminary.AppCityName = CurrentUserInfo.CityName;
            entPreliminary.AppCompanyId = CurrentUserInfo.CompanyId;
            entPreliminary.AppCompanyName = CurrentUserInfo.CompanyName;
            entPreliminary.AppEmployeeCode = CurrentUserInfo.UserCode;
            entPreliminary.PreliminaryDate = DateTime.Now;
        }
        //添加外接预审信息
        else if (entPreliminary.RecordId.Equals(Guid.Empty) && status == "外接")
        {
            entPreliminary.RecordId = Guid.NewGuid();
            entPreliminary.PreliminaryStatus = 10;
            entPreliminary.OperateQuanZ = true;
            entPreliminary.AppCityId = CurrentUserInfo.CityId;
            entPreliminary.AppCityName = CurrentUserInfo.CityName;
            entPreliminary.AppCompanyId = CurrentUserInfo.CompanyId;
            entPreliminary.AppCompanyName = CurrentUserInfo.CompanyName;
            entPreliminary.AppEmployeeCode = CurrentUserInfo.UserCode;
            entPreliminary.EmpCityId = CurrentUserInfo.CityId;
            entPreliminary.EmpCityName = CurrentUserInfo.CityName;
            entPreliminary.EmpCompanyId = CurrentUserInfo.CompanyId;
            entPreliminary.EmpCompanyName = CurrentUserInfo.CompanyName;
            entPreliminary.EmpDepartId = CurrentUserInfo.DepartId;
            entPreliminary.EmpDepartName = CurrentUserInfo.DepartName;
            entPreliminary.EmployeeId = CurrentUserInfo.UserId;
            entPreliminary.EmployeeName = CurrentUserInfo.UserName;
            entPreliminary.EmployeeCode = CurrentUserInfo.UserCode;
            entPreliminary.PreliminaryDate = DateTime.Now;
        }
        //更新预审信息
        else if (status == "更新")
        {
            //预审贷款信息处理业务类
            Chenxin.Lib.Business.BLL.PreliminaryBaseInfo bllPreliminary = new Chenxin.Lib.Business.BLL.PreliminaryBaseInfo();
            Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entOldPreliminary = bllPreliminary.GetModel(entPreliminary.RecordId);
            entPreliminary.RecordId = entOldPreliminary.RecordId;
            entPreliminary.HouseId = entOldPreliminary.HouseId;
            entPreliminary.HouseCode = entOldPreliminary.HouseCode;
            entPreliminary.OperateQuanZ = entOldPreliminary.OperateQuanZ;
            entPreliminary.EmpCityId = entOldPreliminary.EmpCityId;
            entPreliminary.EmpCityName = entOldPreliminary.EmpCityName;
            entPreliminary.EmpCompanyId = entOldPreliminary.EmpCompanyId;
            entPreliminary.EmpCompanyName = entOldPreliminary.EmpCompanyName;
            entPreliminary.EmpDepartId = entOldPreliminary.EmpDepartId;
            entPreliminary.EmpDepartName = entOldPreliminary.EmpDepartName;
            entPreliminary.EmployeeId = entOldPreliminary.EmployeeId;
            entPreliminary.EmployeeName = entOldPreliminary.EmployeeName;
            entPreliminary.EmployeeCode = entOldPreliminary.EmployeeCode;
            entPreliminary.EstimatePrice = entOldPreliminary.EstimatePrice;
            entPreliminary.ProposeBank = entOldPreliminary.ProposeBank;
            entPreliminary.GJJmoney = entOldPreliminary.GJJmoney;
            entPreliminary.GJJyear = entOldPreliminary.GJJyear;
            entPreliminary.SYMoney = entOldPreliminary.SYMoney;
            entPreliminary.SYYear = entOldPreliminary.SYYear;
            entPreliminary.ReplyAdivce = entOldPreliminary.ReplyAdivce;
            entPreliminary.ReplyCompanyId = entOldPreliminary.ReplyCompanyId;
            entPreliminary.ReplyCompanyName = entOldPreliminary.ReplyCompanyName;
            entPreliminary.ReplyDepartId = entOldPreliminary.ReplyDepartId;
            entPreliminary.ReplyDepartName = entOldPreliminary.ReplyDepartName;
            entPreliminary.ReplyUserId = entOldPreliminary.ReplyUserId;
            entPreliminary.ReplyUserName = entOldPreliminary.ReplyUserName;
            entPreliminary.ReplyPreliminaryDate = entOldPreliminary.ReplyPreliminaryDate;
            entPreliminary.LoanApplyDate = entOldPreliminary.LoanApplyDate;
            entPreliminary.OrderPlace = entOldPreliminary.OrderPlace;
            entPreliminary.OrderBank = entOldPreliminary.OrderBank;
            entPreliminary.OrderBeiZhu = entOldPreliminary.OrderBeiZhu;
            entPreliminary.LoanReplyDate = entOldPreliminary.LoanReplyDate;
            entPreliminary.PreliminaryStatus = entOldPreliminary.PreliminaryStatus;
            entPreliminary.delName = entOldPreliminary.delName;
            entPreliminary.delUserId = entOldPreliminary.delUserId;
            entPreliminary.delTime = entOldPreliminary.delTime;
            entPreliminary.ContractId = entOldPreliminary.ContractId;
            entPreliminary.ContractCode = entOldPreliminary.ContractCode;
            entPreliminary.ContractDate = entOldPreliminary.ContractDate;
            entPreliminary.AppCityId = entOldPreliminary.AppCityId;
            entPreliminary.AppCityName = entOldPreliminary.AppCityName;
            entPreliminary.AppCompanyId = entOldPreliminary.AppCompanyId;
            entPreliminary.AppCompanyName = entOldPreliminary.AppCompanyName;
            entPreliminary.AppEmployeeCode = entOldPreliminary.AppEmployeeCode;
            entPreliminary.PreliminaryDate = entOldPreliminary.PreliminaryDate;
            entPreliminary.PreliminaryStatus = 20;
        }

        entPreliminary.PreliminaryApplyDate = DateTime.Now;        
        entPreliminary.LastDate = DateTime.Now;
        entPreliminary.OperateCompanyId = CurrentUserInfo.CompanyId;
        entPreliminary.OperateCompanyName = CurrentUserInfo.CompanyName;
        entPreliminary.OperateDepartId = CurrentUserInfo.DepartId;
        entPreliminary.OperateDepartName = CurrentUserInfo.DepartName;
        entPreliminary.OperateUserId = CurrentUserInfo.UserId;
        entPreliminary.OperateUserName = CurrentUserInfo.UserName;

        return entPreliminary;
    }
}